package com.app.shiyanbiji.interceptor;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import lombok.Getter;
import lombok.Setter;

public class AppAuthInterceptor extends HandlerInterceptorAdapter {

	private static Logger logger = LoggerFactory.getLogger(AppAuthInterceptor.class);

	@Setter@Getter
	private Map<String, Boolean> actions;

	public String getRemoteIP(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	@SuppressWarnings("unchecked")
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String servletPath = request.getServletPath();
		if (actions != null && getActions().containsKey(servletPath) && getActions().get(servletPath)) {
			return true;
		}
		
		String appKey = request.getParameter("App-Key");
		String appSecret = request.getParameter("App-Secret");
		
		if ("hekezui".equals(appKey) && "123".equals(appSecret)) {
		} else {
			PrintWriter writer = response.getWriter();
			response.setContentType("text/plain");
			response.setCharacterEncoding("UTF-8");
			writer.write("You do not have permission to access this page!");
			IOUtils.closeQuietly(writer);
			return false;
		}

//		String ver = request.getHeader("version");
//		String os = request.getHeader("os");
//		String appKey = request.getHeader("App-Key");
//		String appSecret = request.getHeader("App-Secret");
//		String appTime = request.getHeader("App_Time");
//
//		// 查看当前路径是否需要登录,与配置文件中的配置的进行对比,没有配置的为true
//		if (!getActions().containsKey(servletPath) || getActions().get(servletPath)) {
//
//			ObjectMapper objectMapper = new ObjectMapper();
//
//			Map<String, Object> parameterMap = null;
//			try {
//				parameterMap = objectMapper.readValue(request.getInputStream(), Map.class);
//			} catch (Exception e) {
//				parameterMap = new HashMap<String, Object>(0);
//			}
//
//			if (StringUtils.isBlank(appKey) || StringUtils.isBlank(appSecret)) {
//				logger.info("App-Key:" + appKey +"App-Secret:",  appSecret);
//				throw new RuntimeException();
//			}
//		}

		return super.preHandle(request, response, handler);
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {

		super.postHandle(request, response, handler, modelAndView);
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	public static void main(String[] args) {
	}
}